package cn.colins.raft.rpc.handler.chanel;


import cn.colins.raft.enity.RequestVoteResult;
import cn.colins.raft.rpc.RpcRemoteContext;
import cn.colins.raft.rpc.future.RpcWriteFuture;
import cn.colins.raft.utils.ThreadPoolUtils;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 投票请求返回值 处理类
 * @Author czl
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2023/6/12
 */
public class VoteRpcResultHandler extends SimpleChannelInboundHandler<RequestVoteResult> {
    private static final Logger log = LoggerFactory.getLogger(VoteRpcResultHandler.class);



    @Override
    protected void channelRead0(ChannelHandlerContext channel, RequestVoteResult rpcRequest) throws Exception {

        // 获取返回值后 同步响应请求
        ThreadPoolUtils.nettyClientAsyncPool.execute(()->{
            try{
                RpcWriteFuture requestCache = RpcRemoteContext.getRequestCache(rpcRequest.getRequestId());
                if(requestCache!=null){
                    requestCache.setResponse(rpcRequest);
                }
            }finally {
                //释放
                ReferenceCountUtil.release(rpcRequest);
            }
        });
    }


}
